<?php


namespace app\admin\controller;


use think\Db;
use think\Request;

class ConfigController extends CommonController
{
    use ResponseJson;
    private static $CONFIG_TABLE="config";
    private static $UPLOAD_CODE="10001";

    public function GetConf(){
       $res = Db::table(self::$CONFIG_TABLE)
            ->where('delete_flag','=',0)
            ->where('status','=',0)
            ->select();
        foreach ($res as $k=>$v){
            switch ($v['code']){
                case self::$UPLOAD_CODE:
                    //上传配置
                    $data['upload_status']=json_decode($v['value'],true);
            }
        }

        return $this->SuccessResponse('success',$data);

    }

    public function SetConf(Request $request){
        //1为本地上传 2 为七牛云
        $res=$request->param();
        if (!isset($res['upload_status'])||empty($res['upload_status'])){
            return $this->jsonResponse(-1,'上传参数缺失','');
        }
        $upload=[
            'value'=>json_encode(['upload_status'=>$res['upload_status']]),
            'update_by'=>$this->getUserId($request->header('token')),
            'update_time'=>date("Y-m-d H:i:s",time())
            ];
        Db::table(self::$CONFIG_TABLE)
            ->where('code','=',self::$UPLOAD_CODE)
            ->where('delete_flag','=',0)
            ->where('status','=',0)
            ->update($upload);
        return $this->SuccessResponse('更新成功','');
    }

    public function GetUploadConf(){
        $res= DB::table(self::$CONFIG_TABLE)
            ->where('code','=',self::$UPLOAD_CODE)
            ->where('status','=',0)
            ->where('delete_flag','=',0)
            ->value('value');
        $res =json_decode($res,true);
        return $res['upload_status'];
    }

}